local function OnHealthErodeAway(inst)
	local self = inst.components.burnable
	self.fastextinguish = true
	self:KillFX()
end
local function OnKilled(inst)
    local self = inst.components.burnable
    if self ~= nil and self:IsBurning() and not self.nocharring then
        inst.AnimState:SetMultColour(.2, .2, .2, 1)
    end

	--@V2C: #HACK, sync up burn fx to health component's auto-ErodeAway
	if inst.components.health ~= nil and not inst.components.health.nofadeout then
		if self.task ~= nil then
			self.task:Cancel()
		end
		self.task = inst:DoTaskInTime(inst.components.health.destroytime or 2, OnHealthErodeAway)
	end
end
AddComponentPostInit("burnable", function(Burnable)

    local oldIgnite = Burnable.Ignite
    function Burnable:Ignite(immediate, source, doer, ...)
        if source == nil or not source:HasTag("controlled_burner") then
            return oldIgnite(self, immediate, source, doer, ...)
        end

        self.i26laotie = true
        
        if self.burning or self.inst:HasTag("fireimmune") then
            return
        end

        self.controlled_burn = {
            duration_creature = 3,                
            damage = 1.5,
        }
        self.stokeablefire = self.inst.components.health == nil -- NOTES(JBK): There is no benefit to uncontrolled creature fires.

        self:StopSmoldering()

        self.burning = true
        self.inst:ListenForEvent("death", OnKilled)
        self:SpawnFX(immediate)

        self.inst:PushEvent("onignite", {doer = doer})
        if self.onignite ~= nil then
            self.onignite(self.inst, source, doer)
        end

        if self.inst.components.fueled ~= nil and not self.ignorefuel then
            self.inst.components.fueled:StartConsuming()
        end

        if self.inst.components.propagator ~= nil then
            self.inst.components.propagator:StartSpreading(source)
        end

        self:ExtendBurning()
    end

    local oldExtinguish = Burnable.Extinguish
    function Burnable:Extinguish(resetpropagator, heatpct, smotherer, ...)
        if self.burning then
            self.inst:RemoveEventCallback("death", OnKilled)
            self.i26laotie = nil
        end
        return oldExtinguish(self, resetpropagator, heatpct, smotherer, ...)
    end


end)